const reloadMethods = {}

/**
 * 使用示例
  // 父页面
  useTableReload(()=> {
    tableRef.value?.reload?.()
  })
  // 子页面
  function goBack() {
    reloadTable('跳转页面')
    removePageTab({key: getRouteTabKey()})
    push('跳转页面')
  }
 */

export function useTableReload(method) {
  const { currentRoute } = useRouter()
  const currentPath = unref(currentRoute).fullPath

  if (method != null) {
    reloadMethods[currentPath] = { reload: false, method }
  }

  const reloadTable = (path) => {
    if (reloadMethods[path]) {
      reloadMethods[path].reload = true
    }
  }

  onActivated(() => {
    if (reloadMethods[currentPath] && reloadMethods[currentPath].reload) {
      reloadMethods[currentPath].reload = false
      const func = reloadMethods[currentPath].method
      func && func()
    }
  })

  onBeforeUnmount(() => {
    if (reloadMethods[currentPath]) {
      reloadMethods[currentPath].reload = false
      reloadMethods[currentPath].method = void 0
    }
  })

  return { reloadTable }
}
